import { dirname, join, resolve } from "path";
import ver from "./ver.mjs";
import vue from "@vitejs/plugin-vue";
import pkg from "../package.json";
import _plugins from "./plugins";
import pug from "pug";
import title from "./title";
import { writeFileSync } from "fs";
import htmlMinimize from "@sergeymakinen/vite-plugin-html-minimize";

const project = dirname(__dirname);
const root = resolve(join(project, "src/web"));

writeFileSync(
	join(root, "index.html"),
	pug.compileFile(join(root, "index.pug"))({ title }),
);

var plugins = [{ name: "html-transform" }, vue(), ..._plugins];

if (process.env.NODE_ENV == "production") {
	plugins.push(htmlMinimize({ filter: /\.x?html?$/ }));
}

export default {
	esbuild: { target: "chrome" },
	mode: process.env.NODE_ENV,
	root,
	base: "/",
	plugins,
	publicDir: join(project, "public/"),
	build: {
		assetsDir: "",
		emptyOutDir: true,
		outDir: "w",
		target: ["chrome" + ver.chrome.split(".")[0]],
	},
	resolve: {
		alias: {
			"~": join(root, "src"),
			"-": join(dirname(root), "lib"),
			"!": join(dirname(dirname(join(root))), "file"),
		},
	},
};
